package com.atjava.webspider.gui.output;

import java.awt.Color;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

import com.atjava.webspider.consts.Const;
import com.atjava.webspider.gui.task.NewTaskGui;
import com.atjava.webspider.logic.CommonLogic;
import com.atjava.webspider.logic.ConfigLogic;
import com.atjava.webspider.model.Output;
import com.atjava.webspider.model.Site;
import com.atjava.webspider.model.Task;
import com.atjava.webspider.services.TableService;
import com.atjava.webspider.util.DialogUtil;

public class NewOutputGui extends JDialog {

	private static final long serialVersionUID = 1L;
	public static final String [] OUTPUT_STATE = {"不采集","生成XML","生成文本"};
	
	private JPanel jContentPane = null;
	private JLabel jLabelName = null;
	private JLabel jLabelTemplate = null;
	private JLabel jLabelPath = null;
	private JLabel jLabelLevel = null;
	private JTextField jTextFieldName = null;
	private JTextField jTextFieldPath = null;
	private JTextField jTextFieldTemplate = null;
	private JButton jButtonSave = null;
	private JButton jButtonReset = null;
	private JTextArea jTextAreaUrlRegexp = null;
	private JLabel jLabel = null;
	private JButton jButtonBowser = null;
	private JScrollPane jScrollPane = null;
	private JComboBox jComboBoxTmpltType = null;
	private Output output;
	private JLabel jLabelPath1 = null;
	private JComboBox jComboBoxType = null;
	
	/**
	 * @param owner
	 */
	public NewOutputGui(JDialog owner,boolean modal,Output output) {
		super(owner,modal);
		this.output = output;
		initialize();
		setTextValue();
	}
	
	public NewOutputGui(JDialog owner,boolean modal) {
		this(owner,modal,new Output());
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setBounds(new Rectangle(0, 0, 458, 273));
		this.setResizable(false);
		this.setTitle("新建输出");
		this.setLocationRelativeTo(getOwner());
		this.setContentPane(getJContentPane());
	}
	
	private void setTextValue(){
		if(output == null)
			return ;
		try {
			if(StringUtils.isNotBlank(output.getName())){
				jTextFieldName.setText(output.getName());
				jTextFieldName.setEnabled(false);
			}
			if(StringUtils.isNotBlank(output.getPath()))
				jTextFieldPath.setText(output.getPath());
			
			if(StringUtils.isNotBlank(output.getUrlRegexp()))
				jTextAreaUrlRegexp.setText(output.getUrlRegexp());
			
			if(StringUtils.isNotBlank(output.getTemplate()))
				jTextFieldTemplate.setText(output.getTemplate());
			
			if(StringUtils.isNotBlank(output.getPage()))
				jComboBoxType.setSelectedItem(output.getPage());
			
			//设置输出类型下拉框index
			jComboBoxTmpltType.setSelectedIndex(output.getType());
			//设置模板以及路径状态
			setTextState();
			
		} catch (Exception e) {
			e.printStackTrace();
			DialogUtil.showError(this, "加载输出失败！");
		}
	}
	
	private void setTextState(){
		int idx = jComboBoxTmpltType.getSelectedIndex();
		if(idx == 0){
			jTextFieldPath.setEnabled(false);
			jTextFieldPath.setText("");
			jTextFieldTemplate.setEnabled(false);
			jTextFieldTemplate.setText("");
			jButtonBowser.setEnabled(false);
		}
		else if(idx == 1){
			jTextFieldPath.setEnabled(true);
			jTextFieldTemplate.setEnabled(false);
			jTextFieldTemplate.setText("");
			jButtonBowser.setEnabled(false);
		}
		else{
			jTextFieldPath.setEnabled(true);
			jTextFieldTemplate.setEnabled(true);
			jButtonBowser.setEnabled(true);
		}
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabelPath1 = new JLabel();
			jLabelPath1.setBounds(new Rectangle(10, 137, 65, 25));
			jLabelPath1.setText("输出方式：");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(10, 45, 65, 25));
			jLabel.setText("地址规则：");
			jLabelPath = new JLabel();
			jLabelPath.setBounds(new Rectangle(235, 137, 65, 25));
			jLabelPath.setText("输出路径：");
			jLabelLevel = new JLabel();
			jLabelLevel.setBounds(new Rectangle(235, 10, 65, 25));
			jLabelLevel.setText("页面类型：");
			jLabelTemplate = new JLabel();
			jLabelTemplate.setBounds(new Rectangle(10, 172, 65, 25));
			jLabelTemplate.setText("模板文件：");
			jLabelName = new JLabel();
			jLabelName.setBounds(new Rectangle(10, 10, 65, 25));
			jLabelName.setText("输出名称：");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabelName, null);
			jContentPane.add(jLabelTemplate, null);
			jContentPane.add(jLabelPath, null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabelLevel, null);
			jContentPane.add(getJTextFieldName(), null);
			jContentPane.add(getJTextFieldPath(), null);
			jContentPane.add(getJTextFieldTemplate(), null);
			jContentPane.add(getJComboBoxTmpltType(), null);
			jContentPane.add(getJButtonSave(), null);
			jContentPane.add(getJButtonReset(), null);
			jContentPane.add(getJButtonBowser(), null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(jLabelPath1, null);
			jContentPane.add(getJComboBoxType(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextFieldName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldName() {
		if (jTextFieldName == null) {
			jTextFieldName = new JTextField();
			jTextFieldName.setBounds(new Rectangle(75, 10, 150, 25));
		}
		return jTextFieldName;
	}

	/**
	 * This method initializes jTextFieldPath	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldPath() {
		if (jTextFieldPath == null) {
			jTextFieldPath = new JTextField();
			jTextFieldPath.setBounds(new Rectangle(300, 137, 143, 25));
			jTextFieldPath.setEnabled(false);
		}
		return jTextFieldPath;
	}
	
	/**
	 * This method initializes jTextFieldTemplate	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldTemplate() {
		if (jTextFieldTemplate == null) {
			jTextFieldTemplate = new JTextField();
			jTextFieldTemplate.setBounds(new Rectangle(75, 172, 310, 25));
			jTextFieldTemplate.setText(Const.TEMPLATE_HOME.replaceAll("/", "\\\\"));
			jTextFieldTemplate.setEnabled(false);
		}
		return jTextFieldTemplate;
	}
	
	private JComboBox getJComboBoxTmpltType() {
		if (jComboBoxTmpltType == null) {
			jComboBoxTmpltType = new JComboBox(OUTPUT_STATE);
			jComboBoxTmpltType.setBounds(new Rectangle(75, 137, 110, 25));
			jComboBoxTmpltType.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					NewOutputGui.this.setTextState();
				}
			});
		}
		return jComboBoxTmpltType;
	}

	/**
	 * This method initializes jButtonSave	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSave() {
		if (jButtonSave == null) {
			jButtonSave = new JButton();
			jButtonSave.setBounds(new Rectangle(290, 207, 70, 25));
			jButtonSave.setText("保存");
			jButtonSave.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					output.setName(NewOutputGui.this.jTextFieldName.getText());
					output.setPath(NewOutputGui.this.jTextFieldPath.getText());
					String page = (String)NewOutputGui.this.jComboBoxType.getSelectedItem();
					output.setPage(page);
					output.setTemplate(NewOutputGui.this.jTextFieldTemplate.getText());
					output.setType(jComboBoxTmpltType.getSelectedIndex());
					
					output.setUrlRegexp(NewOutputGui.this.jTextAreaUrlRegexp.getText());
					//add into bean
					NewTaskGui newTaskGui = (NewTaskGui)NewOutputGui.this.getOwner();
					//add into table
					try {
						Task t = newTaskGui.getTask();
						if(t.getOutputs() == null)
							t.setOutputs(new ArrayList<Output>());
						int idx = isInList(t.getOutputs());
						TableService tabService = TableService.getInstance();
						//更新
						if(idx >= 0){
							t.getOutputs().set(idx, output);
							newTaskGui.getOutputModel().updateData(idx, tabService.getOutputRow(output));
						//增加
						}else{
							t.getOutputs().add(output);
							newTaskGui.getOutputModel().addData(tabService.getOutputRow(output));
						}
					} catch (Exception exc) {
						exc.printStackTrace();
					}
					NewOutputGui.this.setVisible(false);
				}
			});
		}
		return jButtonSave;
	}
	
	private int isInList(List<Output> outs){
		for (int i = 0; i < outs.size(); i++) {
			Output o = outs.get(i);
			if(o.getName().equals(output.getName())){
				return i;
			}
		}
		return -1;
	}

	/**
	 * This method initializes jButtonReset	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonReset() {
		if (jButtonReset == null) {
			jButtonReset = new JButton();
			jButtonReset.setBounds(new Rectangle(374, 207, 70, 25));
			jButtonReset.setText("取消");
			jButtonReset.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					NewOutputGui.this.setVisible(false);
				}
			});
		}
		return jButtonReset;
	}

	/**
	 * This method initializes jTextAreaUrlRegexp	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextFieldUrlRegexp() {
		if (jTextAreaUrlRegexp == null) {
			jTextAreaUrlRegexp = new JTextArea();
			jTextAreaUrlRegexp.setForeground(new Color(0, 0, 204));
		}
		return jTextAreaUrlRegexp;
	}

	/**
	 * This method initializes jButtonBowser	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonBowser() {
		if (jButtonBowser == null) {
			jButtonBowser = new JButton();
			jButtonBowser.setBounds(new Rectangle(392, 172, 50, 25));
			jButtonBowser.setMargin(new Insets(2, 5, 2, 5));
			jButtonBowser.setEnabled(false);
			jButtonBowser.setText("浏览");
			jButtonBowser.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					JFileChooser jc = new JFileChooser
					(new File(Const.APP_PATH));
					jc.setBackground(Color.WHITE);
					jc.setFileSelectionMode(JFileChooser.OPEN_DIALOG);
					jc.showOpenDialog(NewOutputGui.this);
					File f = jc.getSelectedFile();
					if( f == null )return ;
					NewOutputGui.this.jTextFieldTemplate.setText(f.getAbsolutePath());
				}
			});
		}
		return jButtonBowser;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(75, 45, 368, 79));
			jScrollPane.setViewportView(getJTextFieldUrlRegexp());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jComboBoxType	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxType() {
		if (jComboBoxType == null) {
			NewTaskGui newTaskGui = (NewTaskGui)NewOutputGui.this.getOwner();
			String sitenm = (String)newTaskGui.getJComboBoxSite().getSelectedItem();
			List<Site> sites = ConfigLogic.getSiteConfig().getSites();
			Site st = (Site)CommonLogic.getByName(sites, sitenm);
			String [] pagesNm = new String[st.getPages().size()];
			for (int i = 0; i < pagesNm.length; i++) {
				pagesNm [i] = st.getPages().get(i).getName();
			}
			jComboBoxType = new JComboBox(pagesNm);
			jComboBoxType.setBounds(new Rectangle(300, 10, 143, 25));
		}
		return jComboBoxType;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
